# -*- coding: utf-8 -*-
#
# Author:: hanjunfeng
# Date:: 2022/10/24

def url_to_dict(url):
    url_dict = {}
    url_list = url.split("?")
    adress = url_list[0]
    if len(url_list) > 1:
        params_str = url_list[1]
    else:
        params_str = ""
    params_str_list = params_str.split("&")
    params_dict = {}
    for item in params_str_list:
        tmp_list = item.split("=")
        params_dict.update({tmp_list[0]: tmp_list[1]})

    url_dict.update({"adress": adress})
    url_dict.update({"params": params_dict})
    return url_dict


def dict_to_url(url_dict):
    params_str_list = []
    for item in url_dict["params"].items():
        params_str_list.append(item[0] + "=" + item[1])
        url = url_dict["adress"] + "?" + "&".join(params_str_list)
    return url


def set_url_param(url, params_dict):
    # 解析
    url_dict = url_to_dict(url)
    print(url_dict)
    # 修改
    for key, value in params_dict.items():
        url_dict["params"].update({key: value})
    # 重组
    url = dict_to_url(url_dict)
    return url


if __name__ == "__main__":
    def test_set_url_param():
        url = '''http://query.sse.com.cn/commonSoaQuery.do?jsonCallBack=jsonpCallback20163472&isPagination=true&pageHelp.pageSize=25&pageHelp.pageNo=1&pageHelp.beginPage=1&pageHelp.cacheSize=1&pageHelp.endPage=1&pagecache=false&sqlId=FUND_LIST&fundType=00&subClass=01&_=1666294854949&jsonCallBack=jsonpCallback20163472&isPagination=true&pageHelp.pageSize=25&pageHelp.pageNo=1&pageHelp.beginPage=1&pageHelp.cacheSize=1&pageHelp.endPage=1&pagecache=false&sqlId=FUND_LIST&fundType=00&subClass=01&_=1666294854949'''
        params_dict = {
            "fundType": "09",
            "p1": "123"
        }
        url = set_url_param(url, params_dict)
        print(url)


    test_set_url_param()
